package cn.kpi.service.usualassessment.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.kpi.entity.usualassessment.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.usualassessment.UserUATZpDao;
import cn.kpi.service.usualassessment.IUserUATZpService;

@Service
public class UserUATZpServiceImpl implements IUserUATZpService {

    @Autowired
    private UserUATZpDao userUATZpDao;

    @Transactional
    public int InsertUserUATZp(UserUATZpSummaryEntity entity, Long userid, Long templateid, Map<Long, Integer> map) throws Exception {
        try {
            int result = userUATZpDao.InsertUserUATZpSummary(entity);
            Long usasszpid = entity.getUsasszpid();
            result += userUATZpDao.InsertUserUATZpDetail(usasszpid, userid, templateid,entity.getSzdw(),entity.getUserstatus());
            int icount = 0;
            for (Map.Entry<Long, Integer> entry : map.entrySet()) {
                //result+=userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
                icount++;
            }
            if (result <= (icount + 1)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物

                return -1;
            }
            return result;
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
            ex.printStackTrace();
            return -1;
        }
    }

//	@Transactional
//	public int InsertUserUATZpAtt(UserUATZpSummaryEntity entity,Long userid,Long templateid,Map<Long,Integer> map,UserUATZpAtt entityatt)throws Exception{
//		try{
//			int result=userUATZpDao.InsertUserUATZpSummary(entity);
//			Long usasszpid=entity.getUsasszpid();
//			result+=userUATZpDao.InsertUserUATZpDetail(usasszpid, userid, templateid);
//			int icount=0;
//			for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//			  result+=userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
//			  icount++;
//			}
////			if(result<=(icount+1)){
////				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
////				
////				return -1;
////			}
//			entityatt.setUsasszpid(usasszpid);
//			result+=userUATZpDao.InsertUserUatAtt(entityatt);
//			return result;
//		}
//		catch(Exception ex){
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
//			ex.printStackTrace();
//			return -1;
//		}
//	}


    @Transactional
    public int InsertUserUATZpAtt(UserUATZpSummaryEntity entity, Long userid, Long templateid, Map<Long, Float> map, UserUATZpAtt entityatt) throws Exception {
        try {
            int result = userUATZpDao.InsertUserUATZpSummary(entity);
            Long usasszpid = entity.getUsasszpid();
            Long orgid =entity.getSzdw();
            String status = entity.getUserstatus();
            result += userUATZpDao.InsertUserUATZpDetail(usasszpid, userid, templateid,orgid,status);
            int icount = 0;
            for (Map.Entry<Long, Float> entry : map.entrySet()) {
                result += userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
                icount++;
            }
            entityatt.setUsasszpid(usasszpid);
            result += userUATZpDao.InsertUserUatAtt(entityatt);
            return result;
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int UpdateUserUATZp(UserUATZpSummaryEntity entity, Long userid, Long templateid, Map<Long, Integer> map) throws Exception {
        try {
            int result = userUATZpDao.UpdateUserUATZpSummary(entity);
            Long usasszpid = entity.getUsasszpid();
            int icount = 0;
            for (Map.Entry<Long, Integer> entry : map.entrySet()) {
                //result+=userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
                icount++;
            }
//			if(result<=(icount+1)){
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
//				
//				return -1;
//			}
            return result;
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
            ex.printStackTrace();
            return -1;
        }
    }

//	@Transactional
//	public int UpdateUserUATZpAtt(UserUATZpSummaryEntity entity,Long userid,Long templateid,Map<Long,Integer> map,UserUATZpAtt entityatt)throws Exception{
//		try{
//			int result=userUATZpDao.UpdateUserUATZpSummary(entity);
//			Long usasszpid=entity.getUsasszpid();
//			int icount=0;
//			for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//			  result+=userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
//			  icount++;
//			}
//			result+=userUATZpDao.UpdateUserUATZpAtt(entityatt);
//			return result;
//		}
//		catch(Exception ex){
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
//			ex.printStackTrace();
//			return -1;
//		}
//	}

    @Transactional
    public int UpdateUserUATZpAtt(UserUATZpSummaryEntity entity, Long userid, Long templateid, Map<Long, Float> map, UserUATZpAtt entityatt) throws Exception {
        try {
            int result = userUATZpDao.UpdateUserUATZpSummary(entity);
            Long usasszpid = entity.getUsasszpid();
            int icount = 0;
            for (Map.Entry<Long, Float> entry : map.entrySet()) {
                result += userUATZpDao.UpdateUserUATZpDetailScore(usasszpid, userid, templateid, entry.getKey(), entry.getValue());
                icount++;
            }
            result += userUATZpDao.UpdateUserUATZpAtt(entityatt);
            return result;
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
            ex.printStackTrace();
            return -1;
        }
    }

    public List<UserUATZpDetailEntity> GetUserUATZpDetailList(Long usasszpid, Long userid) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpDetailList(usasszpid, userid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public UserUATZpSummaryEntity GetUserUATZpSummary(Long userid, Long templateid, int year, int quarter) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummary(userid, templateid, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<UserUATZpSummaryEntity> GetUserUATZpSummaryList(Long userid, String where) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryList(userid, where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<UserUATZpSummaryEntity> GetUserUATZpSummaryListTssx(Long userid, String where, int year, int quarter) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryListTssx(userid, where, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetUserUATZpSummaryListCount(Long userid, String where) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryListCount(userid, where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public List<UserUATZpSummaryEntity> GetUserUATZpSummaryListByWhere(String where) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryListByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<UserUATZpSummaryEntity> GetUserUATZpSummaryListByWhereTssx(String where, int year, int quarter) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryListByWhereTssx(where, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetUserUATZpSummaryListCountByWhere(String where) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryListCountByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public int DeleteUATZp(Long usasszpid, Long userid) throws Exception {
        try {
            return userUATZpDao.DeleteUATZp(usasszpid, userid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int DeleteUATZpAtt(Long usasszpid, Long userid) throws Exception {
        try {
            int result = 0;
            result += userUATZpDao.DeleteUATZp(usasszpid, userid);
            result += userUATZpDao.DeleteUserUATAtt(usasszpid);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public UserUATZpSummaryEntity GetUserUATZpSummaryForMzpd(Long userid, int year, int quarter) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryForMzpd(userid, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<UserUATZpDetailEntity> GetZpDetailListRowSpan(Long usasszpid) throws Exception {
        try {
            return userUATZpDao.GetZpDetailListRowSpan(usasszpid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public UserUATZpSummaryEntity GetUserUATZpSummaryById(Long usasszpid) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpSummaryById(usasszpid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int UpdateStatus(Long usasszpid, Long userid, int year, int quarter, int status) throws Exception {
        try {
            int result = 0;
            result += userUATZpDao.AddUsasstempNodeZp(usasszpid, userid, year, quarter);
            result += userUATZpDao.UpdateStatus(usasszpid, status);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int UpdateStatusAtt(Long usasszpid, Long userid, int year, int quarter, int status) throws Exception {
        try {
            int result = 0;
            result += userUATZpDao.AddUsasstempNodeZp(usasszpid, userid, year, quarter);
            result += userUATZpDao.UpdateStatus(usasszpid, status);
            result += userUATZpDao.UpdateUserUATZpAttStatus(usasszpid, status);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public List<UserUATZpSummaryEntity> GetUserZpListProcess(String where, int year, int quarter, int beginrow, int endrow) throws Exception {
        try {

            return userUATZpDao.GetUserZpListProcess(where, year, quarter, beginrow, endrow);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<UserUATZpSummaryEntity> GetOldUserZpListProcess(String where, int year, int quarter, int beginrow, int endrow) throws Exception {
        try {

            return userUATZpDao.GetOldUserZpListProcess(where, year, quarter, beginrow, endrow);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetUserZpListProcessCount(String where) throws Exception {
        try {
            return userUATZpDao.GetUserZpListProcessCount(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public int GetOldUserZpListProcessCount(String where) throws Exception {
        try {
            return userUATZpDao.GetOldUserZpListProcessCount(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public UserUATZpAtt GetUserUATZpAtt(Long usasszpid) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpAtt(usasszpid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public UserUATZpAtt GetUserUATZpAttByWhere(String where) throws Exception {
        try {
            return userUATZpDao.GetUserUATZpAttByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<PSKH> pskhResult(int year, int quarter) {
        return userUATZpDao.pskhResult(year, quarter);
    }
    @Override
    public List<NewPSKH> newpskhResult(int year, int quarter) {
        return userUATZpDao.newpskhResult(year, quarter);
    }

    @Override
    public List<Map> bcjke(int year, int quarter) {
        return userUATZpDao.bcjke(year,quarter);
    }

    @Override
    public List<PSKH> bdjg(int year, int quarter) {
        return userUATZpDao.bdjg(year, quarter);
    }

    @Override
    public List<UserCount> getUserCount() {
        return userUATZpDao.getUserCount();
    }

    @Override
    public int addOpinion(PSKHOpinion pskhOpinion) {
        return userUATZpDao.addOpinion(pskhOpinion);
    }

    @Override
    public int updateOpinion(PSKHOpinion pskhOpinion) {
        return userUATZpDao.updateOpinion(pskhOpinion);
    }

    @Override
    public PSKHOpinion getOpinion(int year, int quarter) {
        return userUATZpDao.getOpinion(year, quarter);
    }

    @Override
    public List<DYStatistics> getDYList(int year, int quarter, int dw) {
        try {
            DYStatistics dyStatistics = new DYStatistics();
            return userUATZpDao.getDYList(year, quarter, dw);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Transactional
    @Override
    public int addBDDC(List<BDDCOpinion> list, int year, int quarter) {

        try {

            if (year > 0 && quarter > 0 && list.size() > 0) {
                userUATZpDao.deleteBDDC(year, quarter);
                for (int i = 0; i < list.size(); i++) {

                    userUATZpDao.addBDDC(list.get(i));
                }

            }

        } catch (Exception ex) {

            return 0;
        }

        return 1;
    }


    @Override
    public List<BDDCOpinion> getBDDC(int year, int quarter) {
        return userUATZpDao.getBDDC(year, quarter);
    }
}

